<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学校名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入学校地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['district:school:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['district:school:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['district:school:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['district:school:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="schoolList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="学校名称" align="center" prop="title" />
      <el-table-column label="城市区域id" align="center" prop="city" />
      <el-table-column label="学校类型" align="center" prop="type" />
      <el-table-column label="缩略图" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="学校地址" align="center" prop="address" />
      <el-table-column label="介绍" align="center" prop="info" />
      <el-table-column label="学校性质" align="center" prop="nature" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['district:school:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['district:school:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学校信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="schoolRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市区域" prop="city">
          <el-select
            label="城市区域"
            v-model="form.city"
            placeholder="请选择"
            prop="city"
            style="width: 240px"
          >
            <el-option
              v-for="item in optionsss"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="学校名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入学校名称" />
        </el-form-item>

        <el-form-item label="学校地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入学校地址" />
        </el-form-item>

        <el-form-item label="学校性质" prop="nature">
          <el-select
            label="学校性质"
            v-model="form.nature"
            placeholder="请选择"
            prop="nature"
            style="width: 240px"
          >
            <el-option
              v-for="item in optionss"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="学校类型" prop="type">
          <el-select
            label="学校类型"
            v-model="form.type"
            placeholder="请选择"
            prop="type"
            style="width: 240px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="缩略图" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>

        <el-form-item label="介绍" prop="info">
          <el-input
            v-model="form.info"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="School">
import {
  listSchool,
  getSchool,
  delSchool,
  addSchool,
  updateSchool,
} from "@/api/district/school";

const { proxy } = getCurrentInstance();
const valuesss = ref("");
const schoolList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    city: null,
    type: null,
    img: null,
    address: null,
    info: null,
    nature: null,
  },
  rules: {
    title: [{ required: true, message: "学校名称不能为空", trigger: "blur" }],
    city: [{ required: true, message: "城市区域不能为空", trigger: "change" }],
    type: [{ required: true, message: "学校类型不能为空", trigger: "change" }],
    img: [{ required: true, message: "缩略图不能为空", trigger: "blur" }],
    address: [{ required: true, message: "学校地址不能为空", trigger: "blur" }],
    info: [{ required: true, message: "介绍不能为空", trigger: "blur" }],
    nature: [
      { required: true, message: "学校性质不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询学校信息列表 */
function getList() {
  loading.value = true;
  listSchool(queryParams.value).then((response) => {
    schoolList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    city: null,
    type: null,
    img: null,
    lat: null,
    lng: null,
    address: null,
    info: null,
    ordid: null,
    status: null,
    grade: null,
    nature: null,
    hits: null,
    seoTitle: null,
    seoKeys: null,
    seoDesc: null,
  };
  proxy.resetForm("schoolRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学校信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getSchool(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学校信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["schoolRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateSchool(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSchool(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除学校信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delSchool(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "district/school/export",
    {
      ...queryParams.value,
    },
    `school_${new Date().getTime()}.xlsx`
  );
}

getList();

const options = [
  {
    value: "公立",
    label: "公立",
  },
  {
    value: "私立",
    label: "私立",
  },
];
const optionss = [
  {
    value: "1",
    label: "幼儿园",
  },
  {
    value: "2",
    label: "小学",
  },
  {
    value: "3",
    label: "初中",
  },
  {
    value: "4",
    label: "高中",
  },
];
const optionsss = [
  {
    value: "1",
    label: "海口市",
  },
  {
    value: "2",
    label: "津南区",
  },
  {
    value: "3",
    label: "西虹市",
  },
  {
    value: "4",
    label: "北辰区",
  },
];
</script>
